var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSendUid: false,
    onClick: false,
    uid: 0,
    otherUid: 0,
    isOnTop: 0,
    position: "",
    position2:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    app.editTabbar();
    this.setData({
      contactList: app.globalData.listContact
    })
  },
  async onTop(e) {
    console.log(e)
    this.setData({
      uid: e.currentTarget.dataset.uid,
      otherUid: e.currentTarget.dataset.otheruid,
      isOnTop: e.currentTarget.dataset.isontop,
    })
    console.log(e.currentTarget.dataset.uid)
    await wx.User.setOnTop({
      data: {
        uid: this.data.uid,
        otherUid: this.data.otherUid,
        isOnTop: this.data.isOnTop,
      }
    })
    await wx.User.listContact()
    this.setData({
      contactList: wx.getStorageSync('contactList')
    })
  },
  async delete(e) {
    console.log(e)
    this.setData({
      uid: e.currentTarget.dataset.uid,
      otherUid: e.currentTarget.dataset.otheruid,
    })
    var that = this
    wx.showModal({
      title: '提示',
      content: '删除该联系人，且同时将您从对方的列表中删除，并清空所有历史消息，是否继续？',
      success: async function (res) {
        if (res.confirm) {
          await wx.User.deleteContact({
            data: {
              uid: that.data.uid,
              otherUid: that.data.otherUid,
            }
          })
          await wx.User.listContact()
          that.setData({
            contactList: wx.getStorageSync('contactList')
          })
        } else {

        }
      }
    })
  },
  sendmessage(e) {
    if (!this.data.onClick) {
      wx.navigateTo({
        url: './sendMessage?uid=' + e.currentTarget.dataset.uid + '&alias=' + e.currentTarget.dataset.alias + '&avatar=' + e.currentTarget.dataset.avatar,
      })
    }
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        onClick: true
      })
    } else {
      this.setData({
        onClick: false
      })
    }
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == "left") {
      this.setData({
        position: "left",
        position2:"left"
      })
    }else if(this.data.ListTouchDirection == null){
      this.setData({
        position: "back"
      })
    }else{
      this.setData({
        position: "back",
        position2: "back"
      })
    }
    if(this.data.position == "back"&&this.data.position2 == "back"){
      this.setData({
        onClick: false
      })
    }
    if(this.data.position == "back"&&this.data.onClick){
      this.setData({
        position2: "back"
      })
    }
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target,
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    app.receiverUnRead();
    await wx.User.listContact()
    this.setData({
      contactList: wx.getStorageSync('contactList')
    })
    var that = this
    wx.onSocketMessage(async function (res) {
      console.log("收到服务器内容：" + res.data)
      if (res.data != '') {
        await wx.User.listContact()
        that.setData({
          contactList: wx.getStorageSync('contactList')
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.closeSocket()
    wx.onSocketClose(function (res) {
      console.log('WebSocket 已关闭！')
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})